﻿using FoodModel;
using J.FoodRepository.InterFace;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace J.FoodRepository.Impl
{
    public class GroupingRepository : BackRepository<GroupingModel>, IGroupingRepository
    {
        private readonly ISqlSugarClient db;

        public GroupingRepository(ISqlSugarClient db) : base(db)
        {
            this.db = db;
        }
        /// <summary>
        /// 菜品分组添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> GroupingAdd(GroupingModel model)
        {
            return await base.AddAsync(model);
        }
        /// <summary>
        /// 菜品分组反填
        /// </summary>
        /// <returns></returns>
        public async Task<GroupingModel> GroupingBack(int id)
        {
            return await base.GetModelAsync(id);
        }
        /// <summary>
        /// 菜品分组删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> GroupingDelete(string id)
        {
            var ids = id.Split(',');
            var list = await base.GetAsync(propa => ids.Contains(propa.GroupingId.ToString()));
            foreach (var item in list)
            {
                base.DeleteAsync(item.GroupingId);
            }
            return list.Count;
        }
        /// <summary>
        /// 菜品分组字段删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> GroupingDeleteid(string id, string GroupingNum)
        {
            var ids = id.Split(',');
            string[] ids1 = GroupingNum.Split(",");
            var list = await base.GetAsync(x => ids.Contains(x.GroupingId.ToString()));
            foreach (var item in list)
            {
                foreach (var Did in ids1)
                {
                    item.GroupingNum = item.GroupingNum.Replace(Did, "");
                }
            }
            foreach (var item in list)
            {
                item.GroupingNum = RemoveExtraCommasAndSpaces(item.GroupingNum);
            }

            return list.Count();
        }
        private string RemoveExtraCommasAndSpaces(string input)
        {
            // 去除多余的逗号和空格
            return string.Join(",", input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
        }
        /// <summary>
        /// 菜品分组显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<GroupingModel>> GroupingShow()
        {
            return await base.GetAllAsync();
        }
        /// <summary>
        /// 菜品分组修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> GroupingUpDate(GroupingModel model)
        {
            return await base.UpdateAsync(model);
        }
        /// <summary>
        /// 菜品分组反填菜品
        /// </summary>
        /// <returns></returns>m
        public async Task<GroupingModel> GroupingBackShow(int id)
        {
            return await base.GetModelAsync(id);
        }
    }
}
